﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace EQual.MainModule.Employees.Views
{
    /// <summary>
    /// Interaction logic for EmployeeCheckBoxView.xaml
    /// </summary>
    public partial class EmployeeCheckBoxView : UserControl, Employees.IEmployeesSelectionView 
    {
        public EmployeeCheckBoxView()
        {
            InitializeComponent();
        }

        IList<CheckBox> boxes;
        IList<DataAccesLayer.Employee> selectedEmnployees;

        public object Model
        {
            get { return selectedEmnployees; }
            set
            {
                selectedEmnployees = value as IList<DataAccesLayer.Employee>;
                SetUpWindow();
                SetUpEmployes(value as IList<DataAccesLayer.Employee>);
                AddEventHandlers();
            }
        }

        private void SetUpEmployes(IList<DataAccesLayer.Employee> iList)
        {
            foreach (DataAccesLayer.Employee empl in iList)
                foreach (CheckBox box in boxes)
                    if (empl.ToString() == box.Tag.ToString())
                        box.IsChecked = true;
        }

        int rows;

        public int Rows
        {
            get { return rows; }
            set { rows = value; }
        }

        private void SetUpWindow()
        {
            IEnumerable<DataAccesLayer.Employee> empls = new DataAccesLayer.EQualDataLayerFactory().GetEmployeeAccesLayer().GetEmployees();

            List<DataAccesLayer.Employee> employees = new List<DataAccesLayer.Employee>(empls);

            boxes = new List<CheckBox>();

            StackPanel Column = new StackPanel();

            double columns = employees.Count / 5;
            columns = Math.Round(columns + 0.5);

            for (int j = 0; j < columns; j++)
            {
                int range = employees.Count - (j) * Rows;
                int rowsf = Rows;
                if (range >= Rows)
                    rowsf = Rows;
                else
                    rowsf = range;// +rows;
                StackPanel columnj = GetColumnWithEmployees(employees.GetRange(j * Rows, rowsf), rowsf);
                AddColumnToGrid(columnj);
            }

        }

        private void AddColumnToGrid(StackPanel columnj)
        {
            this.dockPanel1 .Children.Add(columnj);
        }

        private StackPanel GetColumnWithEmployees(List<DataAccesLayer.Employee> list, int rows)
        {
            StackPanel Column = new StackPanel();
            for (int i = 0; i < rows; i++)
            {
                DataAccesLayer.Employee emp = list[i];

                StackPanel box = new StackPanel();
                //   box.Name = emp.ToString ();

                Label id_l = new Label();
                id_l.Content = emp.ToString();

                CheckBox id_c = new CheckBox();

                // id_c.Name = emp.ToString ();
                id_c.Tag = emp;

                box.Children.Add(id_l);
                box.Children.Add(id_c);
                boxes.Add(id_c);

                Column.Children.Add(box);
            }
            return Column;
        }

        private void AddEventHandlers()
        {
            foreach (CheckBox id_c in boxes)
            {
                id_c.Checked += new RoutedEventHandler(id_c_Checked);
                id_c.Unchecked += new RoutedEventHandler(id_c_Unchecked);
            }
        }

        void id_c_Unchecked(object sender, RoutedEventArgs e)
        {
            CheckBox combo = sender as CheckBox;

            if(ItemDeleted != null)
            this.ItemDeleted(this, new Microsoft.Practices.Composite.Events.DataEventArgs<object>(combo.Tag));
        }

        void id_c_Checked(object sender, RoutedEventArgs e)
        {
            CheckBox combo = sender as CheckBox;

            if(ItemAdded != null)
            this.ItemAdded(this, new Microsoft.Practices.Composite.Events.DataEventArgs<object>(combo.Tag));
        }


        #region IListView<object,object> Members


        public event EventHandler<Microsoft.Practices.Composite.Events.DataEventArgs<object>> ItemSelected;

        public event EventHandler<Microsoft.Practices.Composite.Events.DataEventArgs<object>> ItemDeleted;

        public event EventHandler<Microsoft.Practices.Composite.Events.DataEventArgs<object>> ItemAdded;

        #endregion
    }
}
